package com.fresh.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fresh.common.ResponseResult;
import com.fresh.entity.FreshGoods;
import com.fresh.qo.GoodsQO;
import com.fresh.service.GoodsService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;



    @GetMapping("/all")
    public ResponseResult all(){
        List<FreshGoods> freshGoodsList = goodsService.getAll();
        return ResponseResult.success(freshGoodsList);
    }

    @GetMapping("/goPage")
    public ResponseResult goPage(GoodsQO goodsQO){
        IPage<FreshGoods> goodsIPage = goodsService.getByConditionAndPage(goodsQO);
        return ResponseResult.success(goodsIPage);
    }
    @GetMapping("/selectById")
    public ResponseResult selectById(Integer goodsId){
        FreshGoods goods = goodsService.selectById(Long.valueOf(goodsId));
        return ResponseResult.success(goods);
    }

    @GetMapping  ("/updateById")
    public ResponseResult updateById(Integer goodsId,Integer stock){
        System.out.println(stock);
        goodsService.updateById(Long.valueOf(goodsId),stock);
        return ResponseResult.success();
    }

    /**
     * 初始化秒杀
     * @return
     */
    @GetMapping("/initKill")
    public ResponseResult initKill(Integer goodsId){
        //将该商品所有库存添加至秒杀
        FreshGoods goods = goodsService.selectById(Long.valueOf(goodsId));
        Integer stock = goods.getStock();
        goodsService.initGoodsInRedis(goodsId,stock);
        return ResponseResult.success();
    }

    @PostMapping("/importExcel")
    public ResponseResult add(@RequestBody FreshGoods freshGoods){
        goodsService.importDataToDB(freshGoods);
        return ResponseResult.success();
    }



}
